package com.zhilianbao.leyaogo.ui.fragment.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;

import com.bql.fragmentation.anim.DefaultHorizontalAnimator;
import com.bql.fragmentation.anim.FragmentAnimator;
import com.bql.utils.CheckUtils;
import com.bql.utils.EventManager;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.events.LeEventCode;
import com.zhilianbao.leyaogo.http.api.SupplierApi;
import com.zhilianbao.leyaogo.http.callback.LoadingViewCallback;
import com.zhilianbao.leyaogo.model.response.home.ServiceCity;
import com.zhilianbao.leyaogo.ui.fragment.base.BaseOkHttpFragment;
import com.zhilianbao.leyaogo.view.widgets.TagGroup;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.Response;

/**
 * ClassName: SelectAddressFragment <br>
 * Description: 选择地址Fragment<br>
 * Author: Cyarie <br>
 * Created: 2016/11/1 17:55 <br>
 * Update Time：<br>
 * Update Description：<br>
 */
public class SwitchCityFragment extends BaseOkHttpFragment {

    @BindView(R.id.tg_hot_city)
    TagGroup mTgHotCity;
    @BindView(R.id.ll_service_city)
    LinearLayout mLlServiceCity;
    //    private String address;

    public static SwitchCityFragment getInstance(/*String address*/) {
        SwitchCityFragment switchCityFragment = new SwitchCityFragment();
        //        Bundle bundle = new Bundle();
        //        bundle.putString(IntentConfig.CITY_NAME, address);
        return switchCityFragment;
    }


    @Override
    protected void initToolbarHere() {
        initToolbar(getString(R.string.select_city));
        //        address = getArguments().getString(IntentConfig.CITY_NAME, "");
    }

    @Override
    protected void initViewsAndEvents(Bundle savedInstanceState) {
        getServiceCity();
        //        String testList[] = new String[]{"广州", "上海", "深圳", "南京", "天津", "杭州", "苏州", "无锡", "广州", "上海", "深圳", "南京"};
        //        mTgHotCity.setTags(Arrays.asList(testList));
        //        mTgHotCity.setOnTagClickListener((position, text) -> {
        //            pop();
        //            EventBus.getDefault().post(new EventManager(LeEventCode.MSG_CODE_SELECT_CITY, text + getString(R.string.city_suffix)));
        //        });
    }

    /**
     * 获取服务城市
     */
    private void getServiceCity() {
        SupplierApi.getSupplierCityList(mActivity, "", new LoadingViewCallback<List<ServiceCity>>(mActivity, getView(), false) {
            @Override
            public void errorLeftOrEmptyBtnClick(View v) {
                getServiceCity();
            }

            @Override
            public void onResultSuccess(List<ServiceCity> list, @Nullable Response response, LoadingViewCallback callback) {
                if (!CheckUtils.isEmpty(list)) {
                    mLlServiceCity.setVisibility(View.VISIBLE);
                    List<String> cityList = new ArrayList<>();
                    for (ServiceCity serviceCity : list) {
                        cityList.add(serviceCity.getStrCity());
                    }
                    mTgHotCity.setTags(cityList);
                    mTgHotCity.setOnTagClickListener((position, text) -> {
                        pop();
                        EventBus.getDefault().post(new EventManager(LeEventCode.MSG_CODE_SELECT_CITY, list.get(position)));
                    });
                } else {
                    callback.showEmpty();
                }
            }

        });
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_switch_city;
    }

    @Override
    protected FragmentAnimator onCreateFragmentAnimator() {
        return new DefaultHorizontalAnimator();
    }

}
